@extends('backstage.layout.master')
@section('title', '优惠劵优惠列表')
@section('content')
    <style>
        .state li {
            list-style: none;
            float: left;
            padding: 0 20px;
            text-align: center;
            height: 40px;
            line-height: 40px;
        }

        .state ul {
            margin: 0;
        }

        .state a {
            font-size: 12px;
        }
        .che a {
            font-weight: bold;
            color: #7c7c7c;
        }
    </style>
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a href="backstage/home">主页</a>
            </li>

            <li class="active">
                <a href="/backstage/coupons/couponslist">优惠劵</a>
            </li>
            <li>
                优惠劵列表
            </li>
        </ul><!-- .breadcrumb -->

        <div class="nav-search" id="nav-search">
        </div><!-- #nav-search -->
    </div>

    <div class="page-content">
        <div class="page-header">
            <h1>
                优惠劵
                <small>
                    <i class="icon-double-angle-right"></i>
                    优惠劵列表
                </small>
            </h1>
        </div>
        <div class="row">
            <div class="col-xs-12">
                <div class="well">
                    <h4 class="green smaller lighter">优惠券信息</h4>
                    优惠券名称：{{isset($channel->name) ? $channel->name : ''}}<br>
                    优惠金额：{{isset($channel->price) ? $channel->price : ''}}<br>
                    渠道编号：{{isset($channel->channel_id) ? $channel->channel_id : ''}}<br>
                    优惠类型：@if(isset($channel->coupon_type) && $channel->coupon_type == 1) 优惠满减券
                    @elseif(isset($channel->coupon_type) && $channel->coupon_type == 2) 现金券 @endif<br>
                    优惠券描述：{{isset($channel->desc) ? $channel->desc : ''}}<br>
                </div>
                <!-- PAGE CONTENT BEGINS -->
                <div class="alert alert-info">

                    <i class="icon-hand-right"></i>
                     适用范围添加:&nbsp
                     <select id="type">
                        <option value="product_temp">产品模版</option>
                     </select>
                     <select id="product_temp">
                        @if(isset($resource) && isset($resource['product_temp']) && count($resource['product_temp']) > 0)
                        @foreach($resource['product_temp'] as $temp)
                        <option value="{{isset($temp->product_temp_id) ? $temp->product_temp_id : ''}}">
                            {{isset($temp->name) ? $temp->name : ''}}({{isset($temp->brand_name) ? $temp->brand_name : ''}})
                        </option>
                        @endforeach
                        @else
                        <option value="">已经没有可添加适用范围</option>
                        @endif
                     </select>
                     @if(isset($channel) && $channel->coupon_type == 1)
                     满减额度:
                     <input type="number" id="price" min=0 value="0.00" placeholder="满减额度，保留两位小数" style="width:190px;" id="price" step="0.01">
                     @endif
                     <button onclick="addRang('{{isset($channel->channel_id) ? $channel->channel_id : ''}}')">添加</button>
                     &nbsp&nbsp
                     <button onclick="addAllRang('{{isset($channel->channel_id) ? $channel->channel_id : ''}}')">添加所有</button>
                </div>
                <div class="table-responsive">
                    <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>渠道编号</th>
                            <th>优惠适用资源Id</th>
                            <th>优惠使用资源名</th>
                            <th>优惠类型</th>
                            <th>创建时间</th>
                            <th>更新时间</th>
                            <th>满额度</th>
                            <th width="150px">操作区</th>
                        </tr>
                        </thead>

                        <tbody>
                        @foreach ($channel_maps as $channel_map)
                        <tr class="" data-tr-id="{{isset($channel_map->id)?$channel_map->id:''}}">
                            <td>
                                {{isset($channel_map->id)?$channel_map->id:''}}
                            </td>
                            <td>
                                {{isset($channel_map->channel_id)?$channel_map->channel_id:''}}
                            </td>
                            <td>
                                {{isset($channel_map->reg_id)?$channel_map->reg_id:''}}
                            </td>
                            <td>
                                {{isset($channel_map->res_name)?$channel_map->res_name:''}}
                            </td>
                            <td>
                                @if(isset($channel_map->type)&&$channel_map->type==0)
                                 满额减
                                @elseif(isset($channel_map->type)&&$channel_map->type==1)
                                 产品模版
                                @elseif(isset($channel_map->type)&&$channel_map->type==2)
                                 产品
                                @elseif(isset($channel_map->type)&&$channel_map->type==3)
                                 品牌
                                @endif
                            </td>
                            <td>
                                {{isset($channel_map->created_at)?date('Y-m-d H:i:s',$channel_map->created_at):""}}
                            </td>
                            <td>
                                {{isset($channel_map->updated_at)?date('Y-m-d H:i:s',$channel_map->updated_at):''}}
                            </td>
                            <td style="width:68px;">
                                @if(isset($channel) && $channel->coupon_type == 1)
                                <input type="number" step=0.01 min=0 name="quota" value="{{isset($channel_map->quota)?$channel_map->quota:0}}" class="quota" style="width:68px;">
                                <input type="hidden" value="{{isset($channel_map->quota)?$channel_map->quota:0}}">
                                <input type="hidden" value="{{isset($channel_map->id)?$channel_map->id:0}}">
                                @elseif(isset($channel) && $channel->coupon_type == 2)
                                {{isset($channel_map->quota)?$channel_map->quota:0}}
                                @endif
                            </td>
                            <td class="center" style="padding-top: 15px">
                                <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">
                                    <a class="deletechannel" title="删除" style="color:#d77600;cursor: pointer;" onclick="deleteChannelRes({{$channel_map->id}})">
                                        <i class="icon-trash bigger-130"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dataTables_info" id="sample-table-2_info"></div>
                    </div>
                    <div class="col-sm-6">
                        <div class="dataTables_paginate paging_bootstrap">
                            <?php echo $channel_maps->render(); ?>
                        </div>
                    </div>
                </div>
                <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script type="text/javascript">
        $(function () {
            $(".quota").blur(function(){
                console.log('订单额度在改变');
                var quota = $(this).next().val();
                var newQuota= $(this).val();
                var id = $(this).next().next().val();
                var a = $(this);
                if(!checkDecimals(newQuota)){
                    swal({
                        title: "金额格式不正确!!！",
                        text: "",
                        type: "error",
                        timer:2000
                    });
                }else{
                    $.ajax({
                        type: "POST",
                        url: "/backstage/coupons/channel-res-update-quota",
                        data:{"quota":newQuota,"id":id},
                        success: function(msg){
                            if(msg.state==1){
                                swal({
                                    title: "更新成功",
                                    text: "",
                                    type: "success",
                                    timer:2000
                                });
                            }else {
                                swal({
                                    title: msg.message,
                                    text: "",
                                    type: "error",
                                    timer:2000
                                });
                            }
                        },
                        error:function(){
                            swal({
                                title: "系统繁忙！",
                                text: "",
                                type: "error",
                                timer:3000
                            });
                        }
                    });
                }
            });
        });
        function deleteChannelRes(id) {
            swal({
                title: "确定删除？",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true
            },function (){
                $.ajax({
                    type: "post",
                    url: "/backstage/coupons/channel-res-delete",
                    data:{"id":id},
                    dataType: 'json',
                    success: function(res){
                        if(res.state==1){
                            swal({
                                title:'删除成功！',
                                text: "",
                                type: "success",
                                timer:2000
                            });
                            location.reload();
                        }else{
                            swal({
                                title: res.message,
                                text: "",
                                type: "error",
                                timer:3000
                            });
                        }
                    },
                    error:function(){
                        swal({
                            title: "系统繁忙！",
                            text: "",
                            type: "error",
                            timer:2000
                        });
                    }
                });//ajax
            });
        }
        var state = 0
        function addRang(th){
            if(state == 0){
                state = 1;
                var type = $("#type option:selected").val();
                var resource_id = $("#"+type+" option:selected").val();
                var price = $("#price").val();
                if(price == undefined || price == '') price = 0;
                if(resource_id == undefined || resource_id == ''){
                    swal('已经没有适用范围可添加了～','','warning');
                    state = 0;
                    return ;
                }
                swal('处理中，请稍后','','warning');
                $.ajax({
                    type: "post",
                    url: "/backstage/coupons/add-channel-map",
                    data:{"channel_id":th,"type":type,"resource_id":resource_id,"price":price},
                    dataType: 'json',
                    success: function(res){
                        if(res.state == 'M00000'){
                            @if(isset($channel) && $channel->coupon_type == 1)
                            swal('添加成功,记得填写对应满减额度','','success');
                            @else
                            swal('添加成功','','success');
                            @endif
                            setTimeout(function(){},1000);
                            location.reload();
                            return ;
                        }
                        swal(res.message,'','warning');
                    },
                    error:function(){
                        swal('系统繁忙','请稍后重试','warning');
                    }
                });//ajax
                state = 0;
            }

        }
        function addAllRang(th){
            if(state == 0){
                state = 1;
                add_type = 0;
                var type = $("#type option:selected").val();
                swal('处理中，请稍后','','warning');
                $.ajax({
                    type: "post",
                    url: "/backstage/coupons/add-channel-map",
                    data:{"channel_id":th,"type":type,"add_type":add_type},
                    dataType: 'json',
                    success: function(res){
                        if(res.state == 'M00000'){
                            @if(isset($channel) && $channel->coupon_type == 1)
                            swal('添加成功,记得填写对应满减额度','','success');
                            @else
                            swal('添加成功','','success');
                            @endif
                            setTimeout(function(){},1000);
                            location.reload();
                            return ;
                        }
                        swal(res.message,'','warning');
                    },
                    error:function(){
                        swal('系统繁忙','请稍后重试','warning');
                    }
                });//ajax
                state = 0;
            }

        }
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        /***
         * 小数格式校验
         * @param num
         * @returns {boolean}
         */
        function checkDecimals(num)
        {
            var reg = /^\d+(?=\.{0,1}\d+$|$)/
            return reg.test(num);
        }
    </script>
@stop
